package cn.com.jd.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.util.Calendar;
import java.util.TimeZone;

import javax.annotation.Resource;

import sun.misc.BASE64Encoder;

import org.springframework.stereotype.Service;

import cn.com.jd.common.CommonConstant;
import cn.com.jd.dao.UserInfoDtoMapper;
import cn.com.jd.dto.RegisterDto;
import cn.com.jd.dto.StatusDto;
import cn.com.jd.model.UserInfoDto;
import cn.com.jd.service.RegLogInService;

@Service("RegLogInService")
public class RegLogInServiceImpl implements RegLogInService {
	@Resource
	private UserInfoDtoMapper userInfoDtoMapper;
	
	@Override
	public StatusDto register(RegisterDto rd){
		StatusDto statusDto = new StatusDto();
		int userCount = userInfoDtoMapper.checkExist(rd.getUser_id());
		if (0 != userCount){
			statusDto.setSts(CommonConstant.ERROR);
			statusDto.setMsg(CommonConstant.DUPLICATE_USER);
		} else {
			UserInfoDto uidto = new UserInfoDto();
			uidto.setUserId(rd.getUser_id());
			rd.setPassword(getMD5Str(rd.getPassword()));
			uidto.setUserPassword(rd.getPassword());
			uidto.setInvitedCd(rd.getInvited_cd());
			uidto.setInviteCd((new BASE64Encoder()).encode(rd.getUser_id().getBytes()));
			uidto.setMaxAccount(CommonConstant.GIFT_ACCELERATE_COUNT);
			TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8"); 
			TimeZone.setDefault(tz);
			Calendar cal1 = Calendar.getInstance();
			java.util.Date dt = cal1.getTime();
			cal1.setTime(dt);
			cal1.add(Calendar.DAY_OF_MONTH, CommonConstant.GIFT_DAYS);
			Date expt = new Date(cal1.getTime().getTime());
			uidto.setExpireDate(expt);
			userInfoDtoMapper.insert(uidto);
		}
		return statusDto;
	}
	
	@Override
	public StatusDto login(RegisterDto rd){
		StatusDto statusDto = new StatusDto();
		rd.setPassword(getMD5Str(rd.getPassword()));
		int userCount = userInfoDtoMapper.countUser(rd.getUser_id(),rd.getPassword());
		if (userCount == 0){
			statusDto.setSts(CommonConstant.ERROR);
			statusDto.setMsg(CommonConstant.WRONG_PASSWORD);
		} else {
			statusDto.setSts(CommonConstant.NORMAL);
		}
		return statusDto;
	}
	
	private String getMD5Str(String str) {  
        MessageDigest messageDigest = null;  
  
        try {  
            messageDigest = MessageDigest.getInstance("MD5");  
  
            messageDigest.reset();  
  
            messageDigest.update(str.getBytes("UTF-8"));  
        } catch (NoSuchAlgorithmException e) {  
            System.out.println("NoSuchAlgorithmException caught!");  
            System.exit(-1);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
  
        byte[] byteArray = messageDigest.digest();  
  
        StringBuffer md5StrBuff = new StringBuffer();  
  
        for (int i = 0; i < byteArray.length; i++) {              
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)  
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  
            else  
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  
        }  
  
        return md5StrBuff.toString();  
    }  
}
